﻿using System;

namespace 第二题
{
    using System;

    namespace IndexerSample
    {
        public class Student
        {
            public Student(string number, string name)
            {
                this.Number = number;
                this.Name = name;
            }
            public Student(string number)
            {
                this.Number = number;
                this.Name = "";
            }
            public string Number
            {
                get;
                set;
            }
            public string Name
            {
                get;
                set;
            }

            public void printStudent()
            {
                Console.WriteLine("学号为{0}的同学，叫{1}。", Number, Name);
            }
        }

        class Class
        {
            Student[] _classes;
            public Class(int capacity)
            {
                _classes = new Student[capacity];
            }

            //整数索引器
            public Student this[int index]
            {
                get
                {
                    // 验证索引范围
                    if (index < 0 || index >= _classes.Length)
                    {
                        Console.WriteLine("索引无效");
                        // 使用 null 指示失败
                        return null;
                    }

                    return _classes[index];
                }
                set
                {
                    if (index < 0 || index >= _classes.Length)
                    {
                        Console.WriteLine("索引无效");
                        return;
                    }
                    _classes[index] = value;
                }
            }

            //字符串索引器
            public Student this[string number]
            {
                get
                {
                    // 遍历数组中的所有照片
                    foreach (Student p in _classes)
                    {
                        // 将照片中的标题与索引器参数进行比较
                        if (p.Number == number)
                            return p;
                    }
                    Console.WriteLine("未找到");
                    // 使用 null 指示失败
                    return null;
                }
            }

        }

        class 第二题
        {
            static void Main(string[] args)
            {
                Class school = new Class(3);

                Student first = new Student("202100100", "Jeny");
                Student second = new Student("202100101", "Smith");
                Student third = new Student("202100102", "Lono");

                // 向相册加载照片
                school[0] = first;
                school[1] = second;
                school[2] = third;

                // 按索引检索
                Student objPhoto1 = school[2];
                Console.WriteLine(objPhoto1.Number);

                // 按名称检索
                Student objPhoto2 = school["202100100"];
                Console.WriteLine(objPhoto2.Name);

                Console.ReadLine();
            }
        }
    }
}